﻿# -*- coding: utf-8 -*-

"""
PyoCDVD (http://code.google.com/p/pyocdvd/)
Media Organizer (CD, DVD, etc)

Developers:
    Bruno Pitteli Gonçalves <scorninpc(*)gmail(*)com>
    Paul Eipper <lkraider(*)gmail(*)com>

Licence: GNU GENERAL PUBLIC LICENSE v3

"""

import sys

try:
    import pygtk
    pygtk.require('2.0')
except:
    pass

import gtk


class PyoCDVD_Dialogs(object):
    '''Provide simple dialog windows.'''

    def search_dialog(self):
        return self._textentry_dialog('Insira a palavra-chave', 'Palavra-chave')

    def file_input_dialog(self):
        return self._textentry_dialog('Insira o nome do arquivo', 'Nome')

    def error_dialog(self, text):
        image = gtk.STOCK_DIALOG_WARNING
        self._message_dialog('Aviso!', text, image)

    def device_selection(self, devices):
        title = 'Escolha o Dispositivo'
        label = gtk.Label('Caminho do Dispositivo')
        combo = gtk.combo_box_new_text()
        dialog = self._create_dialog(title, (label, combo))
        for device in devices:
            combo.append_text(device)
        combo.set_active(0)
        response = (dialog.run() == gtk.RESPONSE_OK)
        selection = combo.get_active_text()
        dialog.destroy()
        return response, selection

    def _message_dialog(self, title, text, image):
        image = gtk.image_new_from_stock(image, gtk.ICON_SIZE_DIALOG)
        label = gtk.Label(text)
        label.set_line_wrap(True)
        dialog = self._create_dialog(title, (image, label), ('OK'))
        dialog.run()
        dialog.destroy()

    def _textentry_dialog(self, title, text):
        label = gtk.Label(text)
        entry = gtk.Entry()
        dialog = self._create_dialog(title, (label, entry))
        entry.grab_focus()
        response = (dialog.run() == gtk.RESPONSE_OK)
        text = entry.get_text().strip()
        dialog.destroy()
        return response, text

    def _create_dialog(self, title, elements, buttons=('OK', 'CANCEL')):
        '''Creates a dialog box with the passed elements.

            elements is a list of gtk widgets to display on the dialog;
            buttons defines what buttons the dialog will have.

        '''
        dialog_buttons = []
        if 'CANCEL' in buttons:
            dialog_buttons.append(gtk.STOCK_CANCEL)
            dialog_buttons.append(gtk.RESPONSE_CANCEL)
        if 'OK' in buttons:
            dialog_buttons.append(gtk.STOCK_OK)
            dialog_buttons.append(gtk.RESPONSE_OK)
        dialog = gtk.Dialog(title=title, parent=None,
                            flags=gtk.DIALOG_MODAL|gtk.DIALOG_NO_SEPARATOR,
                            buttons=tuple(dialog_buttons))
        dialog.set_position(gtk.WIN_POS_CENTER)
        dialog.set_resizable(False)
        hbox = gtk.HBox()
        for el in elements:
            hbox.add(el)
            el.show()
        hbox.show()
        dialog.vbox.pack_start(hbox)
        return dialog